home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / gjr / boxes.lha / show.scm < prev    next >
Encoding:
Text File  |  1991-10-02  |  8.7 KB  |  268 lines

  1. ;;; -*- Scheme -*-
  2.  
  3. #|
  4.  
  5. Copyright (c) 1986-91 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case.
  34.  
  35. |#
  36.  
  37. ;;;; Box and pointer diagram printer in Scheme
  38. ;; Uses the Henderson-Escher picture language.
  39.  
  40. (define (show sexp)
  41.   (draw (->picture sexp)))
  42.  
  43. (define (->picture sexp)
  44.   (let ((the-pict (convert sexp)))
  45.     (above (pad (result 1 1 down-arrow) 1 (width the-pict))
  46.            (picture the-pict)
  47.            (/ 1 (1+ (depth the-pict))))))
  48.  
  49. ;; CONVERT returns a data structure (made by RESULT) which contains
  50. ;; the dimensions (depth and width) of the structure as a rectangle
  51. ;; in "box" units (a cons-cell takes 2 boxes, an atom only one).
  52.  
  53. (define result list)
  54. (define depth car)
  55. (define width cadr)
  56. (define picture caddr)
  57.  
  58. (define (convert sexp)
  59.   (if (atom? sexp)
  60.       (result 1 1 (atom-picture sexp))
  61.       (convert-pair (car sexp) (cdr sexp))))
  62.  
  63. (define (convert-pair the-car the-cdr)
  64.   (cond ((and (null? the-car) (null? the-cdr))
  65.          (result 1 2 both-nil-cell))
  66.         ((null? the-car)
  67.          (convert-null-car (convert the-cdr)))
  68.         ((null? the-cdr)
  69.          (convert-null-cdr (convert the-car)))
  70.         ((and (atom? the-car) (atom? the-cdr))
  71.          (convert-both-atoms (convert the-car)
  72.                              (convert the-cdr)))
  73.         (else (convert-general (convert the-car)
  74.                                (convert the-cdr)))))
  75.  
  76. (define (convert-general the-car the-cdr)
  77.   (let ((total-depth (max (+ 2 (depth the-car)) (depth the-cdr)))
  78.         (car-width (max 3 (1+ (width the-car)))))
  79.     (let ((total-width (+ car-width (width the-cdr))))
  80.       (result total-depth
  81.               total-width
  82.               (beside (above (standard-cell car-width)
  83.                              (pad the-car (- total-depth 2) car-width)
  84.                              (/ 2 total-depth))
  85.                       (pad the-cdr total-depth (width the-cdr))
  86.                       (/ car-width total-width))))))
  87.                                      
  88. (define (convert-null-cdr the-car)
  89.   (let ((the-width (max 2 (width the-car)))
  90.         (the-depth (+ 2 (depth the-car))))
  91.     (result the-depth
  92.             the-width
  93.             (above (nil-cdr-cell the-width)
  94.                    (pad the-car (depth the-car) the-width)
  95.                    (/ 2 the-depth)))))
  96.  
  97. (define (convert-null-car the-cdr)
  98.   (if (< (* (+ 2 (depth the-cdr) (1+ (width the-cdr))))
  99.          (* (depth the-cdr) (+ 3 (width the-cdr))))
  100.       (convert-cdr-below the-cdr)
  101.       (convert-cdr-beside the-cdr)))
  102.  
  103. (define (convert-cdr-below the-cdr)
  104.   (let ((the-width (1+ (width the-cdr)))
  105.         (the-depth (+ 2 (depth the-cdr))))
  106.     (result the-depth
  107.             the-width
  108.             (above (nil-car-below-cell the-width)
  109.                    (beside empty (picture the-cdr) (/ 1 the-width))
  110.                    (/ 2 the-depth)))))
  111.  
  112. (define (convert-cdr-beside the-cdr)
  113.   (let ((the-width (+ 3 (width the-cdr)))
  114.         (the-depth (depth the-cdr)))
  115.     (result the-depth
  116.             the-width
  117.             (beside (nil-car-beside-cell the-depth)
  118.                     (picture the-cdr)
  119.                     (/ 3 the-width)))))
  120.  
  121. (define (convert-both-atoms the-car the-cdr)
  122.   (result 3 
  123.           2
  124.           (above both-atom-cell
  125.                  (beside (picture the-car)
  126.                          (picture the-cdr)
  127.                          .5)
  128.                  (/ 2 3))))          
  129.     
  130. (define (pad desc td tw)
  131.   (define (do-one how pict target source)
  132.     (if (= target source)
  133.         pict
  134.         (how pict empty (/ source target))))
  135.   (do-one above
  136.           (do-one beside (picture desc) tw (width desc))
  137.           td
  138.           (depth desc)))
  139.         
  140. (define (standard-cell width)
  141.   (beside left-standard
  142.           (if (= width 3)
  143.               right-standard
  144.               (beside center-standard
  145.                       right-standard
  146.                       (/ (- width 3) (- width 2))))
  147.           (/ 2 width)))
  148.  
  149. (define (nil-cdr-cell width)
  150.   (pad (result 2 2 standard-nil-cdr-cell) 2 width))
  151.  
  152. (define (nil-car-below-cell width)
  153.  (pad (result 2 2 standard-nil-car-below-cell) 2 width))
  154.  
  155. (define (nil-car-beside-cell depth)
  156.   (pad (result 1 3 standard-nil-car-beside-cell) depth 3))
  157.  
  158. (define (atom-picture x)
  159.   (atom-outline
  160.     (cond ((null? x) slash)
  161.           ((symbol? x)
  162.            (text-picture (symbol->string x)))
  163.           ((number? x)
  164.            (text-picture (number->string x '(heur))))
  165.           ((string? x)
  166.            (text-picture 
  167.         (string-append double-quote-string
  168.                (string-append x double-quote-string))))
  169.           (else cross))))
  170.  
  171. (define (atom-outline picture)
  172.   (let ((kernel (together outline picture)))
  173.     (lambda (rectangle)
  174.       (kernel
  175.        (make-rect (+vect (origin rectangle)
  176.                          (scale .05 (+vect (horiz rectangle)
  177.                                            (vert rectangle))))
  178.                   (scale .9 (horiz rectangle))
  179.                   (scale .9 (vert rectangle)))))))
  180.  
  181. ;;;; Primitive pictures
  182.  
  183. ;; text-picture is in cscheme.scm or macscheme.scm, ...
  184.  
  185. (define empty 
  186.   (make-picture '()))
  187. (define slash
  188.   (Make-picture (list (make-segment (make-vect 0 0)
  189.                                     (make-vect 1 1)))))
  190. (define outline
  191.   (make-picture (list (make-segment (make-vect 0 0)
  192.                                     (make-vect 0 1))
  193.                       (make-segment (make-vect 0 1)
  194.                                     (make-vect 1 1))
  195.                       (make-segment (make-vect 1 1)
  196.                                     (make-vect 1 0))
  197.                       (make-segment (make-vect 1 0)
  198.                                     (make-vect 0 0)))))
  199. (define cross
  200.   (make-picture (list (make-segment (make-vect 0 0)
  201.                                     (make-vect 1 1))
  202.                       (make-segment (make-vect 0 1)
  203.                                     (make-vect 1 0)))))
  204.                                     
  205.  
  206. (define down-bar
  207.   (make-picture (list (make-segment (make-vect .5 .5)
  208.                                     (make-vect .5 0)))))
  209.  
  210. (define right-bar (rotate90 down-bar))
  211. (define up-bar (rotate90 right-bar))
  212. (define left-bar (rotate90 up-bar))
  213.  
  214. (define down-arrow
  215.   (make-picture (list (make-segment (make-vect .5 0)
  216.                                     (make-vect .5 1))
  217.                       (make-segment (make-vect .3 .2)
  218.                                     (make-vect .5 0))
  219.                       (make-segment (make-vect .7 .2)
  220.                                     (make-vect .5 0)))))
  221. (define right-arrow (rotate90 down-arrow))
  222. (define up-arrow (rotate90 right-arrow))
  223. (define left-arrow (rotate90 up-arrow))
  224.  
  225. (define arrow-down-cell
  226.   (above (together outline down-bar)
  227.          down-arrow
  228.          .5))
  229.  
  230. (define both-atom-cell
  231.   (beside arrow-down-cell arrow-down-cell .5))
  232.  
  233. (define left-standard
  234.   (beside arrow-down-cell
  235.           (above (together outline right-bar)
  236.                  empty
  237.                  .5)
  238.           .5))
  239.  
  240. (define right-standard
  241.   (above right-arrow empty .5))
  242.  
  243. (define center-standard
  244.    (above (together left-bar right-bar)
  245.           empty
  246.           .5))
  247.  
  248. (define nil-cell
  249.   (together outline slash))
  250.  
  251. (define both-nil-cell
  252.   (beside nil-cell nil-cell .5))
  253.  
  254. (define partial-nil-cell
  255.   (above nil-cell empty .5))
  256.  
  257. (define standard-nil-cdr-cell
  258.   (beside arrow-down-cell partial-nil-cell .5))
  259.  
  260. (define standard-nil-car-below-cell
  261.   (beside partial-nil-cell arrow-down-cell .5))
  262.  
  263. (define standard-nil-car-beside-cell
  264.   (beside nil-cell
  265.           (beside (together outline right-bar)
  266.                   right-arrow
  267.                   .5)
  268.           (/ 1 3)))